package com.runfast.waimai.web.controller;

import com.gxuc.runfast.bean.Business;
import com.runfast.common.dao.model.RunfastCuser;
import com.runfast.common.service.RunfastCuserService;
import com.runfast.common.utils.TokenUtil;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastBusinessCommentService;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastDistributionassessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商户接口
 */
@RestController("userWmBusinessController")
@RequestMapping(value = "/api/user/wm/business", headers = "token")
public class BusinessController {


    @Autowired
    private RunfastBusinessService businessService;

    @Autowired
    private RunfastBusinessCommentService businessCommentService;

    @Autowired
    private RunfastDistributionassessService distributionassessService;

    @Autowired
    private RunfastGoodsSellRecordService goodsSellRecordService;

    @Autowired
    private RunfastCuserService cuserService;

    /**
     * 获取商家详情
     *
     * @param businessId 商户id
     * @param token
     * @return
     */
    @PostMapping("/detail")
    public Result<Map<String, Object>> detail(@RequestParam int businessId, @RequestParam double userLng, @RequestParam double userLat, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);


        Map<String, Object> detail = businessService.detail(businessId, userLng, userLat);
        return Result.ok("", detail);
    }


    /**
     * 添加订单评论
     * @param orderId 订单id
     * @param comment 评论
     * @param businessScore 商家得分
     * @param businessTags 商家标签
     * @param driverScore 骑手得分
     * @param driverTags 骑手标签
     * @param token
     * @return
     */
    @PostMapping("/addComment")
    public Result addComment(@RequestParam int orderId, @RequestParam(required = false) String comment,
                             @RequestParam(required = false) Integer businessScore, @RequestParam(required = false) String businessTags,
                             @RequestParam(required = false) Integer driverScore, @RequestParam(required = false) String driverTags,
                             @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);


        Validate.notBlank(comment, "请说点什么吧");
        Validate.notNull(businessScore, "给个评分吧");

        RunfastGoodsSellRecord goodsSellRecord = goodsSellRecordService.selectByPrimaryKey(orderId);
        if (goodsSellRecord == null) return Result.fail(ResultCode.FAIL, "订单不存在");

        Integer isDeliver = goodsSellRecord.getIsDeliver();
        Integer businessId = goodsSellRecord.getBusinessId();
        RunfastCuser cuser = cuserService.selectByPrimaryKey(userId);

        RunfastBusinessComment businessComment = new RunfastBusinessComment();

        RunfastBusiness bus = businessService.selectByPrimaryKey(businessId);
        businessComment.setAgentId(bus.getAgentId());
        businessComment.setAgentName(bus.getAgentName());

        businessComment.setContent(comment);
        businessComment.setBusinessId(businessId);
        businessComment.setBusinessName(goodsSellRecord.getBusinessName());

        businessComment.setGoodsSellId(goodsSellRecord.getGoodsSellId());
        businessComment.setGoodsSellName(goodsSellRecord.getGoodsSellName());

        businessComment.setOrderCode(goodsSellRecord.getOrderCode());
        businessComment.setGoodsSellRecordId(goodsSellRecord.getId());

        businessComment.setUserId(goodsSellRecord.getUserId());
        businessComment.setUserName(cuser.getNickname());

        businessComment.setCost(goodsSellRecord.getPrice().doubleValue());

        businessComment.setDelicerId(goodsSellRecord.getShopperId());
        businessComment.setDelicerName(goodsSellRecord.getShopper());

        businessComment.setCreateTime(new Date());
        businessComment.setOrderTime(goodsSellRecord.getCreateTime());
        businessComment.setShangstr(businessTags);
        businessCommentService.insert(businessComment);

        RunfastGoodsSellRecord goodsSellRecordUpdate = new RunfastGoodsSellRecord();
        goodsSellRecordUpdate.setId(goodsSellRecord.getId());
        if (isDeliver != null && isDeliver == 1) {

            goodsSellRecordUpdate.setIsComent(1);//是否评价；null：未评价 1：已评价商家， 2：全部评价

        } else {

            //骑手评价
            RunfastDistributionassess distributionAssess = new RunfastDistributionassess();

            //订单
            distributionAssess.setOrderNumber(goodsSellRecord.getOrderCode());
            distributionAssess.setGoodsSellRecordId(goodsSellRecord.getId());

            //评价人
            distributionAssess.setAssessId(goodsSellRecord.getUserId());
            distributionAssess.setAssessman(cuser.getNickname());


            distributionAssess.setAssessTime(new Date());
            distributionAssess.setConsume(goodsSellRecord.getPrice());//金额

            //评价信息
            distributionAssess.setScore(Double.valueOf(driverScore));
            distributionAssess.setContent(comment);

            //配送人员
            distributionAssess.setShopperId(goodsSellRecord.getShopperId());
            distributionAssess.setShopperName(goodsSellRecord.getShopper());
            distributionAssess.setDistributionTime(goodsSellRecord.getReadyTime());

            distributionAssess.setAgentId(bus.getAgentId());
            distributionAssess.setAgentName(bus.getAgentName());
            distributionAssess.setQishoustr(driverTags);
            distributionassessService.insert(distributionAssess);

            goodsSellRecordUpdate.setIsComent(2);//是否评价；null：未评价 1：已评价商家， 2：全部评价

        }

        goodsSellRecordService.updateByPrimaryKey(goodsSellRecordUpdate);

        return Result.ok("");
    }

    /**
     * 获取商家下的用户评论
     *
     * @param businessId 商户id
     * @param pageable
     * @param token
     * @return
     */
    @PostMapping("/listComment")
    public Result<List<RunfastBusinessComment>> listComment(@RequestParam int businessId, @PageableDefault Pageable pageable, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);

        RunfastBusinessCommentExample businessCommentExample = new RunfastBusinessCommentExample();
        businessCommentExample.or().andBusinessIdEqualTo(businessId);
        businessCommentExample.setOrderByClause("createTime desc");

        List<RunfastBusinessComment> businessCommentList = businessCommentService.selectByExampleWithPageable(businessCommentExample, pageable);

        return Result.ok("",businessCommentList);
    }

}
